package controllers

import (
	"comment/models"
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

type RoleController struct {
	BaseController
}

func (c *RoleController) List() {
	c.Data["PageTitle"] = "角色列表"
	pageNo, _ := c.GetInt("PageNo")
	if pageNo == 0 {
		pageNo = 1
	}
	sizePage := c.GetString("SizePage")
	s, _ := strconv.Atoi(sizePage)
	if s == 0 {
		s = 10
	}
	c.Data["SizePage"] = s
	c.Data["Page"] = models.PageRoles(pageNo, s)
	c.Layout = "layout/layout.html"
	c.TplName = "role/role.html"
}

func (c *RoleController) Add() {
	beego.ReadFromRequest(&c.Controller)
	c.Data["PageTitle"] = "添加角色"
	c.Layout = "layout/layout.html"
	c.TplName = "role/add.html"
}

func (c *RoleController) Save() {
	flash := beego.NewFlash()
	name := c.GetString("Name")
	if len(name) == 0 {
		flash.Error("角色名称不能为空")
		flash.Store(&c.Controller)
		c.Redirect("/role/add", 302)
	} else {
		role := models.Role{}
		if err := c.ParseForm(&role); err != nil {
			flash.Error("角色创建失败")
			flash.Store(&c.Controller)
			c.Redirect("/role/add", 302)
		} else {
			_, err := models.SaveRole(&role)
			if err != nil {
				flash.Error("角色保存失败", err)
				flash.Store(&c.Controller)
				c.Redirect("/role/add", 302)
			} else {
				c.Redirect("/role/list", 302)
			}

		}

	}
}

func (c *RoleController) Edit() {
	beego.ReadFromRequest(&c.Controller)
	flash := beego.NewFlash()
	c.Data["PageTitle"] = "角色修改"
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	if id > 0 {
		c.Data["Role"] = models.FindRoleById(id)
		c.Layout = "layout/layout.html"
		c.TplName = "role/roleEdit.html"
	} else {
		flash.Error("角色不存在")
		flash.Store(&c.Controller)
		c.Redirect("/role/edit/"+strconv.Itoa(id), 302)
	}
}

func (c *RoleController) Update() {
	flash := beego.NewFlash()
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	if id > 0 {
		name := c.GetString("Name")
		if len(name) == 0 {
			flash.Error("角色名称不能为空")
			flash.Store(&c.Controller)
			c.Redirect("/role/edit/"+strconv.Itoa(id), 302)
		} else {
			role := models.Role{Id: id}
			if err := c.ParseForm(&role); err != nil {
				flash.Error("角色修改失败")
				flash.Store(&c.Controller)
				c.Redirect("/role/edit/"+strconv.Itoa(id), 302)
			} else {
				count := models.UserCountByRole(id)
				if count != 0 && role.Status == 0 {
					flash.Error("当前角色下存在用户,不允许禁用")
					flash.Store(&c.Controller)
					c.Redirect("/role/edit/"+strconv.Itoa(id), 302)
				} else {
					models.UpdateRole(&role)
					c.Redirect("/role/list", 302)
				}
			}
		}
	} else {
		flash.Error("角色不存在")
		flash.Store(&c.Controller)
		c.Redirect("/role/edit/"+strconv.Itoa(id), 302)
	}

}

func (c *RoleController) PermEdit() {
	beego.ReadFromRequest(&c.Controller)
	c.Data["PageTitle"] = "权限分配"
	flash := beego.NewFlash()
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	if id > 0 {
		admin := c.GetString("admin")
		r := models.FindRoleById(id)
		if strings.Contains(r.Name, "超级管理") && admin != "Admin" {
			c.Ctx.WriteString("你没有权限访问这个页面")
		} else {
			c.Data["Role"] = r
			c.Data["RolePermissions"] = models.FindRolePermissionByRoleId(id)
			permissions := models.FindPermissionsByPid(0)
			for _, p := range permissions {
				p.ChildPermissions = models.FindPermissionsByPid(p.Id)
			}
			c.Data["Permissions"] = permissions
			c.Layout = "layout/layout.html"
			c.TplName = "role/edit.html"
		}
	} else {
		flash.Error("角色不存在")
		flash.Store(&c.Controller)
		c.Redirect("/role/permedit/"+strconv.Itoa(id), 302)
	}
}

func (c *RoleController) PermUpdate() {
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	per := c.Ctx.Input.Query("permissionIds")
	permissionIds := strings.Split(per, ",")
	models.DeleteRolePermissionByRoleId(id)
	for _, pid := range permissionIds {
		_pid, _ := strconv.Atoi(pid)
		models.SaveRolePermission(id, _pid)
	}
	c.Redirect("/role/list", 302)
}

func (c *RoleController) Delete() {
	id, _ := strconv.Atoi(c.Ctx.Input.Param(":id"))
	if id > 0 {
		count := models.UserCountByRole(id)
		if count == 0 {
			role := models.Role{Id: id}
			models.DeleteRole(&role)
			c.Redirect("/role/list", 302)
		} else {
			c.Ctx.WriteString("当前角色下存在用户,不能删除")
		}

	} else {
		c.Ctx.WriteString("角色不存在")
	}
}
